package com.sad.jetpack.v1.datamodel.api.extension.client.socket.ipc;

import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONObject;

public class IPCMember {
    private String clientId = RandomStringUtils.random(32);//端id
    private String address="";//端地址
    private String description="";//端描述
    private String Alias ="";//端代号
    private JSONObject extra=new JSONObject();//附加信息
    private IPCMember(){}
    public IPCMember(String clientId) {
        this.clientId = clientId;
    }

    public String getAddress() {
        return address;
    }

    public static IPCMember newInstance(){
        return new IPCMember();
    }

    public IPCMember setAddress(String address) {
        this.address = address;
        return this;
    }

    public String getClientId() {
        return clientId;
    }

    public IPCMember setClientId(String clientId) {
        this.clientId = clientId;
        return this;
    }

    public String getDescription() {
        return description;
    }

    public IPCMember setDescription(String description) {
        this.description = description;
        return this;
    }

    public String getAlias() {
        return Alias;
    }

    public IPCMember setAlias(String alias) {
        this.Alias = alias;
        return this;
    }

    public JSONObject getExtra() {
        return extra;
    }

    public IPCMember setExtra(JSONObject extra) {
        this.extra = extra;
        return this;
    }

    public static IPCMember parse(JSONObject jsonObject){
        IPCMember member=new IPCMember();
        member.setClientId(jsonObject.optString("id"));
        member.setAddress(jsonObject.optString("address"));
        member.setAlias(jsonObject.optString("alias"));
        member.setDescription(jsonObject.optString("description"));
        member.setExtra(jsonObject.optJSONObject("extra"));
        return member;
    }

    public JSONObject toJson(){
       try {
           JSONObject json=new JSONObject();
           json.put("id", getClientId());
           json.put("address",getAddress());
           json.put("alias",getAlias());
           json.put("description",getDescription());
           json.put("extra",getExtra());
           return json;
       }catch (Exception e){
           e.printStackTrace();
       }
       return null;
    }
}
